package cn.b.sky.mes.andon.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.andon.enums.AndonConfigType;
import cn.b.sky.mes.andon.enums.AndonTypeStatus;
import cn.b.sky.mes.andon.model.AndonAccount;
import cn.b.sky.mes.andon.model.AndonConfig;
import cn.b.sky.mes.andon.model.AndonType;
import cn.b.sky.mes.andon.service.AndonAccountService;
import cn.b.sky.mes.andon.service.AndonConfigService;
import cn.b.sky.mes.andon.service.AndonTypeService;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * <strong>Title : AndonConfigController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.andon.controller<br></strong>
 * <strong>Description : </strong>工作中心物料ANDON配置<br> 
 * <strong>Create on : 2016-9-14 上午9:11:58<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/andonConfig/*")
public class AndonConfigController extends SecurityController {

  @Resource
  AndonTypeService andonTypeService;

  @Resource
  private WorkCenterService workCenterService;

  @Resource
  private WorkUnitService workUnitService;

  @Resource
  AndonAccountService andonAccountService;

  @Resource
  AndonConfigService andonConfigService;
 

  @RequestMapping("andonConfig_list")
  public String andonAndonlist(Model model, String navTabId) {
    
    List<AndonType> andonTypes = andonTypeService.getAndonAccountListBySiteCode(this.getSiteCode(), new Integer(
        AndonTypeStatus.ENABLE.getValue()));
    // 组装工作中心
    List<WorkCenter> workCenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);
    Map<String, List<WorkCenter>> centerMap = new HashMap<String, List<WorkCenter>>();
    centerMap.put("", new LinkedList<WorkCenter>());
    for (WorkCenter workCenter : workCenters) {
      if (StringUtils.isEmpty(workCenter.getParentWorkCenterCode())) {
        centerMap.get("").add(workCenter);
      } else {
        if (!centerMap.containsKey(workCenter.getParentWorkCenterCode())) {
          centerMap.put(workCenter.getParentWorkCenterCode(), new LinkedList<WorkCenter>());
        }
        centerMap.get(workCenter.getParentWorkCenterCode()).add(workCenter);
      }
    }
    // 组装作业单元
    List<WorkUnit> workUnits = workUnitService.getAllWorkUnitListBySiteCode(getSiteCode(), 0);
    Map<String, List<WorkUnit>> centerUnitMap = new HashMap<String, List<WorkUnit>>();
    for (WorkUnit workUnit : workUnits) {
      if (!centerUnitMap.containsKey(workUnit.getWorkCenterCode())) {
        centerUnitMap.put(workUnit.getWorkCenterCode(), new LinkedList<WorkUnit>());
      }
      centerUnitMap.get(workUnit.getWorkCenterCode()).add(workUnit);
    }
    List<WorkCenter> workCentersTree = centerMap.get("");
    setChildren(centerMap, centerUnitMap, workCentersTree);
    model.addAttribute("workCentersTree", workCentersTree);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("andonTypes", andonTypes);
    return "andon/andonConfig/andonConfig_list";
  }

  @RequestMapping("getMaterialCodeByAndonType")
  @ResponseBody
  public DWZResult getMaterialCodeByAndonType(String code) {
    if (StringUtils.isBlank(code)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    List<AndonAccount> accounts = andonAccountService.getMaterialCodeByAndonType(getSiteCode(), code);
    return ajaxDone(accounts);
  }

  @RequestMapping("getAndonConfig")
  @ResponseBody
  public DWZResult getAndonConfig(String type, String code) {
    if (StringUtils.isBlank(type) || StringUtils.isBlank(code)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    return ajaxDone(andonConfigService.getAndonConfigByCode(type, getSiteCode(), code));
  }

  @RequestMapping("saveAndonConfig")
  @ResponseBody
  public DWZResult saveAndonConfig(String navTabId, String andonConfigs, String type, String workUnitCode,
      String workCenterCode) {
    if (StringUtils.isBlank(andonConfigs) && StringUtils.isBlank(type)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    Map<String, Object> object = JSONObject.parseObject(andonConfigs);
    @SuppressWarnings("unchecked")
    List<Map<String, Object>> maps = (List<Map<String, Object>>) object.get("elements");
    String map = null;
    for (int i = 0; i < maps.size(); i++) {
      if (i == 0) {
        map = maps.get(i).get("value").toString();
      } else {
        map = map + "," + maps.get(i).get("value").toString();
      }
    } 
    List<AndonConfig> configs = null;
    if (map != null) {
      configs = JSON.parseArray("["+map+"]", AndonConfig.class);
    }
    if (AndonConfigType.WORKCENTER.getValue().equals(type)) {      
      andonConfigService.saveWcAndonConfig(getSiteCode(), configs, workCenterCode);
    } else if (AndonConfigType.WORKUNIT.getValue().equals(type)) {
      andonConfigService.saveWuAndonConfig(getSiteCode(), configs, workUnitCode);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
  }
  
  /**
   * 
   * <strong>Title : setChildren<br></strong>
   * <strong>Description : </strong>设置工作中心作业单元树结构<br> 
   * <strong>Create on : 2016年9月10日 上午11:02:03<br></strong>
   * <p>
   * @param centerMap
   * @param centerUnitMap
   * @param workCenters
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void setChildren(Map<String, List<WorkCenter>> centerMap, Map<String, List<WorkUnit>> centerUnitMap,
      List<WorkCenter> workCenters) {
    for (WorkCenter workCenter : workCenters) {
      if (centerMap.containsKey(workCenter.getWorkCenterCode())) {
        workCenter.setChildrens(centerMap.get(workCenter.getWorkCenterCode()));
        setChildren(centerMap, centerUnitMap, workCenter.getChildrens());
      }
      if (centerUnitMap.containsKey(workCenter.getWorkCenterCode())) {
        workCenter.setWorkUnits(centerUnitMap.get(workCenter.getWorkCenterCode()));
      }
    }
  }

}
